package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import src.Spawner;
	
	public class DefensaKuelap extends Sprite
	{
		private var myRocas:Array = new Array;
		private var countFrame:uint = 0;
		private var t:uint = 5;
		private var rdm:Number = 1;
		private var ramValue1:Number = 100;
		private var spawner:Spawner;
		private var maxHP:int = 100;
		private var currentHP:int = maxHP;
		private var percentHP:Number = currentHP/maxHP;
		public function DefensaKuelap()
		{
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			
		}
		private function onAddedToStage(event:Event):void{
			spawner = new Spawner(this);
			addChild(spawner);
			spawner.x = this.width;
			spawner.y = -spawner.height/2;
			
			
			stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			
		}
		private function updateHealthBar():void{
			percentHP = currentHP / maxHP;
			if (currentHP > 0){
				healthBar.barColor.scaleX = percentHP;
			}else{
				healthBar.barColor.scaleX = 0;
			}
			
		}
		private function onEnterFrame(event:Event):void{
			
			updateHealthBar();
			countFrame++;
			if (countFrame == Math.floor(stage.frameRate*rdm)){
				rdm= Math.random()*5+5;
				countFrame=0;
				ramValue1 = Math.floor(Math.random()*100);
				spawner.spawn(ramValue1);
				trace(spawner.pos);
			}
			for (var i:uint = 0; i < myRocas.length; i++){
				myRocas[i].updateRoca();
				
				if (kuelap.hitTestObject(myRocas[i])){
					currentHP -= 25;
					removeChild(myRocas[i]);
					myRocas.splice(i,1);
				}
			}

		}
		
		
		public function get rocas():Array{
			return myRocas;	
		}
		public function set rocas(value:Array):void{
			myRocas = value;
		}
	}
}